package com.yumeno.education.controller;

import com.yumeno.education.entity.Questions;
import com.yumeno.education.entity.Student;
import com.yumeno.education.entity.Teacher;
import com.yumeno.education.service.QuestionsService;
import com.yumeno.education.service.StudentService;
import com.yumeno.education.service.TeacherService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @Resource
    private QuestionsService questionsService;

    @RequestMapping("/loginPage")
    public ModelAndView loginPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("teacher/login");
        return modelAndView;
    }

    @RequestMapping("/teacherIndex")
    public ModelAndView studentIndex() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("teacher/teacherIndex");
        return modelAndView;
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "index";
    }

    @RequestMapping("/login")
    ModelAndView login(Teacher teacher) {
        ModelAndView mv = new ModelAndView();
        if (StringUtils.isEmpty(teacher.getUsername()) || StringUtils.isEmpty(teacher.getPassword())) {
            mv.addObject("errMsg1", "用户名或密码为空！");
            mv.setViewName("/teacher/login");
        }else{
            Teacher dbTeacher = teacherService.login(teacher);
            if (dbTeacher != null) {
                mv.addObject("teacher", dbTeacher);
                mv.setViewName("teacher/teacherIndex");
            } else {
                mv.addObject("errMsg1", "账号或密码错误！");
                mv.setViewName("/teacher/login");
            }
        }
        return mv;
    }

    @RequestMapping("/add")
    @ResponseBody
    Map<String,Object> add(Questions questions) {
        System.out.println(questions);
        return questionsService.add(questions);
    }

}
